package com.ecoolex.ying.space.admin.graphql;

import com.ecoolex.framework.common.pojo.Page;
import com.ecoolex.framework.common.pojo.Pagination;
import com.ecoolex.framework.common.pojo.Sorting;
import com.ecoolex.framework.common.util.Assert;
import com.ecoolex.framework.common.util.Check;
import com.ecoolex.framework.modularize.api.auth.admin.AdminMemberAuthority;
import com.ecoolex.framework.modularize.api.auth.annotation.AdminAuthoritativeRegister;
import com.ecoolex.framework.modularize.api.service.storage.StorageService;
import com.ecoolex.framework.web.api.graphql2.annotation.*;
import com.ecoolex.ying.space.admin.core.AdminConstant;
import com.ecoolex.ying.space.entity.GoodsClassify;
import com.ecoolex.ying.space.entity.GoodsLabel;
import com.ecoolex.ying.space.pojo.AddGoodsLabelDTO;
import com.ecoolex.ying.space.pojo.EditGoodsLabelDTO;
import com.ecoolex.ying.space.pojo.QueryGoodsLabelDTO;
import com.ecoolex.ying.space.ws.GoodsService;
import com.ecoolex.ying.space.ws.PickGoodsService;
import org.apache.dubbo.config.annotation.Reference;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.lang.NonNull;

import static com.ecoolex.framework.web.api.graphql2.annotation.GraphQL2Module.Type.Admin;
import static com.ecoolex.ying.space.admin.graphql.GoodsLabelGraphql.AUTH;

@GraphQL2Module(type = Admin, description = "商品标签")
@AdminAuthoritativeRegister(parentKey = AdminConstant.AUTH_MODULE_KEY, key = AUTH, name = "商品标签")
public class GoodsLabelGraphql {
    static final String AUTH = AdminConstant.AUTH_MODULE_KEY + "-GOODS-LABEL";
    static final String AUTH_GET = AUTH + "-GET";
    static final String AUTH_QUERY = AUTH + "-QUERY";
    static final String AUTH_ADD = AUTH + "-ADD";
    static final String AUTH_EDIT = AUTH + "-EDIT";
    static final String AUTH_DEL = AUTH + "-DEL";

    @Reference
    private GoodsService goodsService;
    @Reference
    private PickGoodsService pickGoodsService;
    @Autowired
    private StorageService storageService;

    @TypeField(type = GoodsLabel.class, field = "canEdit", description = "能否编辑")
    public boolean canEdit(@Source GoodsLabel source, AdminMemberAuthority user) {
        return user.auth(AUTH_EDIT);
    }

    @TypeField(type = GoodsLabel.class, field = "canDel", description = "能否删除")
    public boolean canDel(@Source GoodsLabel source, AdminMemberAuthority user) {
        return user.auth(AUTH_DEL);
    }

    @TypeField(type = GoodsLabel.class, field = "usedGoodsNumber", description = "使用标签的商品数量")
    public long usedGoodsNumber(@Source GoodsLabel source) {
        return goodsService.countGoodsByLabelId(source.getLabelId());
    }

    @TypeField(type = GoodsLabel.class, field = "usedPickGoodsNumber", description = "使用标签的优选商品数量")
    public long usedPickGoodsNumber(@Source GoodsLabel source) {
        return pickGoodsService.countPickGoodsByLabelId(source.getLabelId());
    }

    @QueryField(field = "goodsLabel", description = "获取商品标签详情")
    @AdminAuthoritativeRegister(parentKey = AUTH, key = AUTH_GET, name = "获取商品标签详情")
    public GoodsLabel get(@NonNull Integer labelId, AdminMemberAuthority user) {
        user.assertAuth(AUTH_GET);
        return goodsService.getLabelById(labelId);
    }

    @QueryField(field = "goodsLabelList", description = "查询商品标签列表")
    @AdminAuthoritativeRegister(parentKey = AUTH, key = AUTH_QUERY, name = "查询商品标签列表")
    public Pagination<GoodsLabel> query(@NonNull QueryGoodsLabelDTO query, @NonNull Sorting[] sorts, @NonNull Page page, AdminMemberAuthority user) {
        user.assertAuth(AUTH_QUERY);
        return goodsService.queryLabel(query, sorts, page);
    }


    @MutationField(field = "addGoodsLabel", description = "添加商品标签")
    @AdminAuthoritativeRegister(parentKey = AUTH, key = AUTH_ADD, name = "添加商品标签")
    public GoodsLabel add(@NonNull AddGoodsLabelDTO add, AdminMemberAuthority user) {
        user.assertAuth(AUTH_ADD);
        return goodsService.createLabel(add, user.userName());
    }

    @MutationField(field = "editGoodsLabel", description = "编辑商品标签")
    @AdminAuthoritativeRegister(parentKey = AUTH, key = AUTH_EDIT, name = "编辑商品标签")
    public boolean edit(@NonNull EditGoodsLabelDTO edit, AdminMemberAuthority user) {
        user.assertAuth(AUTH_EDIT);
        goodsService.updateLabel(edit, user.userName());
        return true;
    }

    @MutationField(field = "delGoodsLabel", description = "删除商品标签")
    @AdminAuthoritativeRegister(parentKey = AUTH, key = AUTH_DEL, name = "删除商品标签")
    public boolean del(@NonNull Integer labelId, AdminMemberAuthority user) {
        user.assertAuth(AUTH_DEL);
        GoodsLabel record = goodsService.getLabelById(labelId);
        Assert.isTrue(Check.notNull(record), "商品标签不存在");
        goodsService.deleteLabel(record, user.userName());
        return true;
    }


}
